using System.Collections;
using UnityEngine;

public class Entity_VFX : MonoBehaviour
{
    [SerializeField] private Material onHitMaterial;
    [SerializeField] private float onDamageVFXDuration = 0.1f;
    protected SpriteRenderer spriteRenderer;
    private Material originalMaterial;
    private Coroutine onDamageVFXCoroutine;
    private WaitForSeconds waitTime;

    [Header("Hit VFX")]
    [SerializeField] private GameObject hit_VFX;
    [SerializeField] private GameObject hit_CritVFX;


    [Header("Element Color")]
    [SerializeField] private Color chillVFXColor = Color.cyan;
    [SerializeField] private Color burnVFXColor = Color.red;
    [SerializeField] private Color lightningColor = Color.yellow;
    private Color initChillVFXColor;
    private Color initBurnVFXColor;
    private Color initLightningVFXColor;
    protected virtual void Awake()
    {
        spriteRenderer = GetComponentInChildren<SpriteRenderer>();
        originalMaterial = spriteRenderer.material;
        waitTime = new WaitForSeconds(onDamageVFXDuration);
        initChillVFXColor = chillVFXColor;
        initBurnVFXColor = burnVFXColor;
        initLightningVFXColor = lightningColor;
    }

    //特效处理
    public void EffectHandler(Transform attacker, bool isCrit, ElementType elementType)
    {
        //显示受击特效
        PlayOnDamageVFX();

        //创建击中特效
        CreateHitVFX(attacker, isCrit, elementType);
    }
    public void CreateItemEffectVFX(GameObject vfx, Transform target)
    {
        GameObject vfxPrefab = PoolManager.Instance.Release(vfx, (vfxPrefab) =>
        {
            vfxPrefab.transform.position = target.position;
            vfxPrefab.transform.rotation = Quaternion.identity;
        });
    }
    public void PlayOnDamageVFX()
    {
        if (onDamageVFXCoroutine != null)
            StopCoroutine(onDamageVFXCoroutine);
        onDamageVFXCoroutine = StartCoroutine(DamageVFXCo());
    }
    public void PlayOnStatusVFX(float duration, ElementType elementType)
    {
        switch (elementType)
        {
            case ElementType.Ice:
                StartCoroutine(PlayStatusVFXCo(duration, chillVFXColor));
                break;
            case ElementType.Fire:
                StartCoroutine(PlayStatusVFXCo(duration, burnVFXColor));
                break;
            case ElementType.Lightning:
                StartCoroutine(PlayStatusVFXCo(duration, lightningColor));
                break;
        }
    }
    private IEnumerator PlayStatusVFXCo(float duration, Color effectColor)
    {
        float tickInterval = .25f;
        float timer = 0;
        Color lightColor = effectColor * 1.2f;
        Color drakColor = effectColor * .9f;
        bool toggle = false;
        while (timer < duration)
        {
            spriteRenderer.color = toggle ? lightColor : drakColor;
            toggle = !toggle;
            yield return new WaitForSeconds(tickInterval);
            timer += tickInterval;
        }
        spriteRenderer.color = Color.white;
    }
    private IEnumerator DamageVFXCo()
    {
        spriteRenderer.material = onHitMaterial;
        yield return waitTime;
        spriteRenderer.material = originalMaterial;
    }
    public void StopAllVFXCoroutine()
    {
        StopAllCoroutines();
        spriteRenderer.color = Color.white;
        spriteRenderer.material = originalMaterial;
    }
    public void CreateHitVFX(Transform attacker, bool isCrit, ElementType elementType)
    {
        GameObject VFX = isCrit ? hit_CritVFX : hit_VFX;
        //从对象池取出对象
        GameObject hitVFXPrefab = PoolManager.Instance.Release(VFX, (hitVFXPrefab) =>
        {
            hitVFXPrefab.transform.position = transform.position;
            hitVFXPrefab.transform.rotation = Quaternion.identity;
        });

        //通过计算攻击者和受击者的相对位置，来判定攻击特效是否翻转
        if (transform.position.x - attacker.position.x < 0 && isCrit)
            hitVFXPrefab.transform.Rotate(0, 180f, 0);
    }
    public Color GetElementColor(ElementType elementType)
    {
        switch (elementType)
        {
            case ElementType.Ice: return chillVFXColor;
            case ElementType.Fire: return burnVFXColor;
            case ElementType.Lightning: return lightningColor;
            default: return Color.white;
        }
    }
    public Color GetInitColor(ElementType elementType)
    {
        switch (elementType)
        {
            case ElementType.Ice: return initChillVFXColor;
            case ElementType.Fire: return initBurnVFXColor;
            case ElementType.Lightning: return initLightningVFXColor;
            default:return Color.white;
        }
    }
}
